/*********************************************************************\
*
*                  (c) lkc
*                  All Rights Reserved. lkc Confidential
*
\*********************************************************************/
/*********************************************************************\
*   Title                      :
*
*   Module Description         :
*
*   Author                     : lkc
*
*   Created                    : 2018-7-24
*
\*********************************************************************/

#include "Communicate.h"
#include "ProtocolMCU.h"
#include "ProtocolPC.h"

#ifdef POD_IS_205
#include "ProtocolPod205.h"
#else
#include "ProtocolPod613.h"
#endif //#ifdef POD_IS_205

#include "SerialPortPosix.h"
#include "UDPSockPosix.h"

std::map<int, ProtocolBasePosix*> protocolMap;

bool CommInitPosix()
{
    //MCU通信设置
#ifdef MCU_IS_ALIVE
#ifndef MCU_COMM_TYPE_IS_QT
//    static SerialPortPosix commMCU(DEV_NAME_MCU);
    std::shared_ptr<SerialPortPosix> commMCU = std::make_shared<SerialPortPosix>(DEV_NAME_MCU);

    if(!commMCU->Open())
    {
        return false;
    }
    if(!commMCU->SetOpt(B19200,0,8,1,'N'))
    {
        printf("Set Port failed\n");
        return false;
    }
    ProtocolMCU::GetInstance().SetCommTool(commMCU);
    protocolMap[commMCU->GetFd()] = &ProtocolMCU::GetInstance();
    ProtocolMCU::GetInstance().CycleSendThread();
#endif//#ifndef MCU_COMM_TYPE_IS_QT
#endif//#ifdef MCU_IS_ALIVE

    //PC通信设置
#ifdef PC_IS_ALIVE
#ifndef PC_COMM_TYPE_IS_QT
#ifdef PC_COMM_IS_SER
    //普通指针new了之后必须delete，否则产生野指针。或者也可用static变量的形式。智能指针可以避免这种情况。
    std::shared_ptr<SerialPortPosix> commPC = std::make_shared<SerialPortPosix>(DEV_NAME_PC);
    if(!commPC->Open()) //打开串口，返回文件描述符,上位机串口
    {
        return false;
    }
    if(!commPC->SetOpt(B115200,0,8,1,'E'))
    {
        printf("Set Port failed\n");
        return false;
    }
#else
//    static UDPSockPosix commPC(NET_PORT_PC);
    std::shared_ptr<UDPSockPosix> commPC = std::make_shared<UDPSockPosix>(NET_PORT_PC);
    if(!commPC->Open())
    {
        return false;
    }
#endif //PC_COMM_IS_SER
    ProtocolPC::GetInstance().SetCommTool(commPC);
    protocolMap[commPC->GetFd()] = &ProtocolPC::GetInstance();
    ProtocolPC::GetInstance().CycleSendThread();
#endif //#ifndef PC_COMM_TYPE_IS_QT
#endif //#ifdef PC_IS_ALIVE

    //Pod通信设置
#ifndef POD_COMM_TYPE_IS_QT
    //普通指针new了之后必须delete，否则产生野指针。或者也可用static变量的形式。智能指针可以避免这种情况。
    std::shared_ptr<SerialPortPosix> commPod = std::make_shared<SerialPortPosix>(DEV_NAME_POD);
    if(!commPod->Open()) //打开串口，返回文件描述符,613串口
    {
        return false;
    }
    if(!commPod->SetOpt(B115200,0,8,1,'E'))
    {
        printf("Set Port failed\n");
        return false;
    }
#ifdef POD_IS_205
    ProtocolPod205::GetInstance().SetCommTool(commPod);
    protocolMap[commPod->GetFd()] = &ProtocolPod205::GetInstance();
    ProtocolPod205::GetInstance().CycleSendThread();
#else
    ProtocolPod613::GetInstance().SetCommTool(commPod);
    protocolMap[commPod->GetFd()] = &ProtocolPod613::GetInstance();
    ProtocolPod613::GetInstance().CycleSendThread();
#endif//#ifdef POD_IS_205
#endif//#ifndef POD_COMM_TYPE_IS_QT

    return true;
}

/********************************************************************\
 *
 * REVISION RECORDS
 *
\*********************************************************************/
/*********************************************************************/
/*
 *
 *
 *
\*********************************************************************/
/*------------------------------The End------------------------------*/
